#!/bin/bash
menu(){
echo -e "\e[36m************system useradmin Menu************\e[0m"
echo -e "\e[31m		1.add user \e[0m"
echo -e "\e[32m		2.del user \e[0m"
echo -e "\e[33m		3.query user information \e[0m"
echo -e "\e[34m		4.reset user passwd \e[0m"
echo -e "\e[35m		5.modify user information \e[0m"
echo -e "\e[36m		6.show all user information \e[0m"
echo -e "\e[31m 	7.exit the program \e[0m"
}
#1.add user
add_user(){
	read -p "please input your name: " u_name
	if id $u_name &>/dev/null
	then
		echo " $u_name is exists"
	else
		useradd $u_name
		echo "$u_name create is OK" 
	fi
}

#2.del user
del_user(){
	read -p "please input your name: " u_name
	if id $u_name &>/dev/null
	then
		userdel -r $u_name &>/dev/null
		echo "del $u_name is Success "
	else
		echo "$u_name is exists"
	fi
}
#3.query user information
query_user_information(){
	read -p "please input your name: " u_name
	if id $u_name &>/dev/null
	then
		id $u_name
	echo " `cat /etc/passwd|egrep $u_name`"
	
	a=`cat /etc/shadow|awk -F: '{print $2}'`
	elif [[ $a == !! ]]
	then 
		echo "$u_name 没有设置密码"
	else
		echo "$u_name 已经设置密码"
	fi

}
#4.reset user passwd
reset_user_passwd(){
	read -p "please input your name: " u_name
        if id $u_name &>/dev/null
	then
	read -p "please input your passwd: " u_passwd
		echo $u_passwd|passwd $u_name --stdin &>/dev/null
		echo "$u_name create passwd is OK"
	else
		echo "$u_name is not exists"
	fi
}
#5.modify user information
modify_user_information(){
	read -p "please input your name: " u_name
	if  id $u_name &>/dev/null
	then
		echo -e "\e[35m`id $u_name`\e[0m"
	while true
	do
	echo 
	read -p "please input you want to modify ?(1.uid/2.gid/3.home_dir)" choose
	case $choose in
	1)
		read -p "please input your uid: " u_uid
		usermod -u $u_uid $u_name
		echo -e "\e[32m`id $u_name`\e[0m"
		a=`cat /etc/passwd|egrep "caocao"|awk -F: '{print $3}'`
		if [[ $a=$u_uid ]] 
		then
		echo "create uid ok"
		fi
		;;
	2)
		read -p "please input your gid: " u_gid
		groupmod -g $u_gid caocao
		echo -e "\e[35m`id $u_name`\e[0m"
		 echo "create gid ok"
		;;
	3)
		read -p "please input your home dir: " home_dir
		[ -d $home_dir ]&& echo "目录存在不需要创建"||mkdir $home_dir &>/dev/null
		usermod -d $home_dir $u_name
		echo "`cat /etc/passwd |egrep $u_name|awk -F: '{print $6}'`"
		echo "$home_dir create ok "
		break
		;;
	*)
		break
		;;
		esac
	done
	fi
}
#6.show all user information
show_all_user_infromation(){
		a=`cat /etc/passwd|wc -l`
		b=`cat /etc/passwd|awk -F: '$3<=499&&$3>0{print $1}'|wc -l`
		c=`cat /etc/passwd|awk -F: '$3>=500{print $1}'|wc -l`
		echo "用户总数：" $a
		echo "系统用户个数：" $b
		echo "普通用户个数：" $c
		echo 	
}
###############################################
main(){
  clear
  while true 
  do
  menu
  read -p"  please choose the option(1-7) :" choice
  case $choice in
  1)
        add_user
	;;
  2)
	del_user
	;;
  3)
	query_user_information
	;;
  4)
	reset_user_passwd
	;;
  5)    
	modify_user_information
	;;
  6)
	show_all_user_infromation
	;;
  7)
	break
	;;
  *)    break
	;;
 
  esac
   read -p "please put any key to continue " key
   clear 
  done
}
main


